package com.ztgf.shopping.api.client.shop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.shopping.models.bo.shop.ShopQualificationAuditBrandListBO;
import com.ztgf.shopping.models.bo.shop.ShopQualificationAuditIsBO;
import com.ztgf.shopping.models.bo.shop.ShopQualificationAuditSaveListBO;
import com.ztgf.shopping.models.vo.shop.ShopQualificationAuditBrandVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import com.ztgf.shopping.models.service.shop.IShopQualificationAuditService;
import com.ztgf.shopping.models.dto.shop.ShopQualificationAuditDTO;

import java.util.List;
/**
 * 店铺资质审核表控制器
 *
 * @author zhanghualang
 * @Date 2020-06-23 11:58:06
 */

@RestController
@RequestMapping("/api/shopQualificationAudit")
@Slf4j
@Api(value = "ShopQualificationAuditApiController",tags = "店铺资质审核表Api接口")
public class ShopQualificationAuditApiController {

    @DubboReference
    private IShopQualificationAuditService shopQualificationAuditService;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<ShopQualificationAuditDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        ShopQualificationAuditDTO dto = shopQualificationAuditService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "根据店铺ID查询营业执照审核表",notes = "根据店铺ID查询营业执照审核表")
    @PostMapping("/getLicenseAudit")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<ShopQualificationAuditDTO> getLicenseAudit(Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        Wrapper<ShopQualificationAuditDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("shop_id", id);
        //类型 0营业执照，1法人身份证，2开户许可证，3商标注册证书，4续展证明文件，5ISO认证书，6品牌授权书
        wrapper.eq("type", 0);
        //审核类型 0注册，1开店，2资质变更，3新增品牌，4新增类目
        wrapper.andNew().eq("audit_type", 2).or().eq("audit_type", 0);
        wrapper.orderBy("create_time", false); //降序
        wrapper.last("limit 1");
        ShopQualificationAuditDTO dto = shopQualificationAuditService.selectOne(wrapper);

        return new Result<>(dto);
    }

    @ApiOperation(value = "根据店铺ID或用户ID查询营业执照审核表",notes = "根据店铺ID或用户ID查询营业执照审核表")
    @PostMapping("/getLicenseAuditBySU")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shopId",value = "shopId",required = true,paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "userId",value = "userId",required = true,paramType = "query", dataType = "Long")
    })
    public Result<ShopQualificationAuditDTO> getLicenseAuditBySU(@RequestParam("shopId") Long shopId, @RequestParam("userId") Long userId){
        if(shopId == null && userId == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        Wrapper<ShopQualificationAuditDTO> wrapper = new EntityWrapper<>();

        //类型 0营业执照，1法人身份证，2开户许可证，3商标注册证书，4续展证明文件，5ISO认证书，6品牌授权书
        wrapper.eq("type", 0);
        //审核类型 0注册，1开店，2资质变更，3新增品牌，4新增类目
        wrapper.andNew().eq("audit_type", 2).or().eq("audit_type", 0);

        wrapper.andNew().eq("user_id", userId).or().eq("shop_id", shopId);
        wrapper.orderBy("create_time", false); //降序
        wrapper.last("limit 1");
        ShopQualificationAuditDTO dto = shopQualificationAuditService.selectOne(wrapper);

        return new Result<>(dto);
    }

    @ApiOperation(value = "根据店铺ID查询基础资质审核表列表",notes = "根据店铺ID查询基础资质审核表列表")
    @PostMapping("/queryBaseList")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<List<ShopQualificationAuditDTO>> queryBaseList(Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        Wrapper<ShopQualificationAuditDTO> wrapper = new EntityWrapper<>();
        //审核类型 0注册，1开店，2资质变更，3新增品牌，4新增类目
        wrapper.eq("shop_id", id).eq("audit_type", 2);
        //审核状态 0审核中，1审核通过，2审核打回，3审核不通过
        wrapper.eq("status", 0);
        //类型 0营业执照，1法人身份证，2开户许可证，3商标注册证书，4续展证明文件，5ISO认证书，6品牌授权书
        wrapper.andNew().eq("type", 0).or().eq("type",1).or().eq("type", 2);
        List<ShopQualificationAuditDTO> shopQualificationAuditDTOList = shopQualificationAuditService.selectList(wrapper);

        return new Result<>(shopQualificationAuditDTOList);
    }


    @ApiOperation(value = "根据店铺ID查询店铺资质审核表列表",notes = "根据店铺ID查询基础资质审核表列表")
    @PostMapping("/queryBrandList")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<List<ShopQualificationAuditBrandVO>> queryBrandList(Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        List<ShopQualificationAuditBrandVO> vos = shopQualificationAuditService.queryBrandList(id);
        return new Result<>(vos);
    }

    @ApiOperation(value = "验证资质是否已经提交了审核",notes = "验证资质是否已经提交了审核")
    @PostMapping("/isAudit")
    public Result isAudit(ShopQualificationAuditIsBO bo){
        if(bo.getShopId() == null || bo.getType() == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        boolean flag = shopQualificationAuditService.isAudit(bo);

        if (flag){
            return new Result<>(CodeEnum.FAIL_100041);
        }
        return new Result<>();
    }

    @ApiOperation(value = "提交资质变更审核",notes = "提交资质变更审核")
    @PostMapping("/addChange")
    public Result addChange(ShopQualificationAuditSaveListBO bo){
        boolean flag = shopQualificationAuditService.addChange(bo);
        if (flag){
            return new Result<>();
        }else{
            return new Result<>(CodeEnum.FAIL_100012);
        }
    }

    @ApiOperation(value = "新增品牌",notes = "新增品牌")
    @PostMapping("/addBrand")
    public Result addBrand(ShopQualificationAuditBrandListBO bo){
        boolean flag = shopQualificationAuditService.addBrand(bo);
        if (flag){
            return new Result<>();
        }else{
            return new Result<>(CodeEnum.FAIL_100012);
        }
    }

    @ApiOperation(value = "验证资质是否已经提交了审核",notes = "验证资质是否已经提交了审核")
    @PostMapping("/isQAudit")
    public Result isQAudit(ShopQualificationAuditIsBO bo){
        if(bo.getShopId() == null || bo.getType() == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        boolean flag = shopQualificationAuditService.isQAudit(bo);

        if (flag){
            return new Result<>(CodeEnum.FAIL_100041);
        }
        return new Result<>();
    }
}
